package day_14.homework;

/**
 * @author ck
 * @date2021/8/22 22:13
 */
@SuppressWarnings("all")
public class Send implements Runnable{
    public int getGift() {
        return gift;
    }

    public void setGift(int gift) {
        this.gift = gift;
    }

    public Send(int gift) {
        this.gift = gift;
    }

    public Send() {
    }

    //定义礼物总数
    private int gift = 100;
    //同步方法
    private synchronized void sendGift(){
               try {
                   //休眠可能会有异常所以要抓取
                   Thread.sleep(50);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               if (gift > 10) {
                   gift--;
                   System.out.println(Thread.currentThread().getName() +"送出了第"+ (100 - this.getGift() + "份礼物"));
               }else {
                   //终止虚拟机
                   System.exit(0);
               }
           }

    //重写run方法
    @Override
    public void run() {
        while (true){
            //调用sendGift方法
            sendGift();
        }
    }
}
